/**
 * 
 */
package edu.sjtu.seda.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import edu.sjtu.seda.stage.StageWrapper;

/**
 * @author john.zhu
 *
 */
public class ThreadPoolImpl implements ThreadPool {
	
	private ExecutorService threadpool;
	private StageWrapper wrapper;
	private int coreThreadPoolSize;
	
	public ThreadPoolImpl(StageWrapper wrapper){
		this(wrapper,5);
	}
	
	public ThreadPoolImpl(StageWrapper wrapper,int corePoolSize){
		if(threadpool!=null)
			throw new RuntimeException("Threadpool already exists!");
		this.coreThreadPoolSize = corePoolSize;
		threadpool = Executors.newFixedThreadPool(coreThreadPoolSize);
		this.wrapper = wrapper;
	}
	
	@Override
	public void execute(Runnable runner) {
		threadpool.execute(runner);
	}

	@Override
	public void shutdown() {
		threadpool.shutdown();
		try {
			threadpool.awaitTermination(2, TimeUnit.MINUTES);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isTerminated() {
		return false;
	}
	
}
